/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dataanalysis.gui.dataviewer;

import java.awt.Dimension;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableModel;
import weka.core.Instances;

/**
 *
 * @author Administrator
 */
abstract public class BaseTableView extends JScrollPane {

    protected TableContent m_tableContent;
    private TableRowHeader m_tableRowHeader;
    private int nRows;
    private int nCols;

    public BaseTableView(int r, int c) {
        nRows = r;
        nCols = c;
        m_tableContent = new TableContent(createTableModel(r, c));
        m_tableRowHeader = new TableRowHeader(r);
        m_tableRowHeader.setBackground(m_tableContent.getTableHeader().getBackground());
        m_tableContent.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        m_tableContent.getTableHeader().setReorderingAllowed(false);
        m_tableRowHeader.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        this.setLayout();
        this.setViewportView(m_tableContent);
        this.setRowHeaderView(m_tableRowHeader);
        this.setVisible(true);
    }

    public void setInstances(Instances data) {
        if (data != null && data.classIndex() == -1) {
            data.setClassIndex(data.numAttributes() - 1);
        }
        m_tableRowHeader.setInstances(data);
        nRows = m_tableRowHeader.getRowCount();
        nCols = m_tableRowHeader.getColumnCount();
        this.setLayout();
        this.adjustLayout();
    }

    protected void setCellSize(int w, int h) {
        m_tableContent.setMinWidth(w);
        m_tableContent.setRowHeight(h);
        setRowHeaderSize(h);
    }

    private void setRowHeaderSize(int h) {

        m_tableRowHeader.getColumnModel().getColumn(0).setMinWidth(80);
        m_tableRowHeader.setRowHeight(h);
        m_tableRowHeader.setPreferredScrollableViewportSize(new Dimension(80, h * nRows));
    }

    protected void setMargin(int x, int y) {
        m_tableContent.setRowMargin(x);
        m_tableContent.getColumnModel().setColumnMargin(y);
        m_tableRowHeader.setRowMargin(x);
        m_tableRowHeader.getColumnModel().setColumnMargin(y);
    }

    abstract protected void setLayout();

    abstract protected TableModel createTableModel(int r, int c);

    abstract protected void adjustLayout();
}
